//
//  NSDateExtra.m
//  majordomo
//
//  Created by luc on 14-11-27.
//  Copyright (c) 2014年 luc. All rights reserved.
//

#import "NSDateExtra.h"

@implementation NSDateExtra


+ (NSInteger)getTimeIntervalWithStartTime:(NSString *)startTime endTime:(NSString *)endTime{
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSDate *startDate = [formatter dateFromString:startTime];
    NSDate *endDate = [formatter dateFromString:endTime];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit unit = NSCalendarUnitSecond;
    NSDateComponents *cmps = [calendar components:unit fromDate:startDate toDate:endDate options:0];
    return cmps.second;
}


+ (NSString *)time_timestampToString:(NSInteger)timestamp{
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
    NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"yyyy-MM-dd"];
    NSString* string=[dateFormat stringFromDate:confromTimesp];
    return string;
}

+(NSDate *)getDateWithDateString:(NSString *)time
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateFormat:@"YYYY-MM-dd"];//hh与HH的区别:分别表示12小时制,24小时制
    NSDate *confromTimesp = [dateFormatter dateFromString:time];;
    
    return confromTimesp;
}

//NSDtate 转YYYY-MM-dd
+(NSString *)getTimeYYMMDDStringWithDate:(NSDate *)time
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    [dateFormatter setDateFormat:@"YYYY-MM-dd"];//hh与HH的区别:分别表示12小时制,24小时制
    NSString *confromTimespStr = [dateFormatter stringFromDate:time];
    
    return confromTimespStr;
}
//NSDtate 转HH:mm:ss
+(NSString *)getTimeHHmmssStringWithDate:(NSDate *)time
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    [dateFormatter setDateFormat:@"HH:mm:ss"];//hh与HH的区别:分别表示12小时制,24小时制
    NSString *confromTimespStr = [dateFormatter stringFromDate:time];
    
    return confromTimespStr;
}


+(NSString *)getConfromWithDateString:(NSString *)time
{
    return [self getConfromWithDateString:time withDateFormat:@"YYYY-MM-dd HH:mm"];
}
+(NSString *)getConfromWithDateString:(NSString *)time withDateFormat:(NSString *)dateFormat
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateFormat:dateFormat];//hh与HH的区别:分别表示12小时制,24小时制
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[time doubleValue]];
    NSString *confromTimespStr = [dateFormatter stringFromDate:confromTimesp];
    
    return confromTimespStr;
}


+(NSString *)getTimeStringWithDateString:(NSString *)time
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];//hh与HH的区别:分别表示12小时制,24小时制
    NSDate *confromTimesp = [dateFormatter dateFromString:time];
    [dateFormatter setDateFormat:@"YY/MM/dd HH:mm"];//hh与HH的区别:分别表示12小时制,24小时制
    NSString *confromTimespStr = [dateFormatter stringFromDate:confromTimesp];
    
    return confromTimespStr;
}
//NSDtate 转YY/MM/dd HH:mm
+(NSString *)getTimeStringWithDate:(NSDate *)time
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY/MM/dd HH:mm"];//hh与HH的区别:分别表示12小时制,24小时制
    NSString *confromTimespStr = [dateFormatter stringFromDate:time];
    
    return confromTimespStr;
}

#pragma mark- 日期字符串转NSDate
+(NSDate *)getDateWithTimeString:(NSString *)time dateFormat:(NSString *)dateFormat
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateFormat:dateFormat];//hh与HH的区别:分别表示12小时制,24小时制
    NSDate *confromTimesp = [dateFormatter dateFromString:time];;
    
    return confromTimesp;
}
#pragma mark- NSDate转日期字符串
+(NSString *)getDataTimeStringWithDate:(NSDate *)time dateFormat:(NSString *)dateFormat
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    [dateFormatter setDateFormat:dateFormat];//hh与HH的区别:分别表示12小时制,24小时制
    NSString *confromTimespStr = [dateFormatter stringFromDate:time];
    
    return confromTimespStr;
}

#pragma mark- 将日期字符串转换成想要的日期字符串
+(NSString *)getDateStringWithDateString:(NSString *)time
                        currenDateFormat:(NSString *)currenDateFormat
                              dateFormat:(NSString *)dateFormat
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    [dateFormatter setDateFormat:currenDateFormat];//hh与HH的区别:分别表示12小时制,24小时制
    NSDate *confromTimesp = [dateFormatter dateFromString:time];
    [dateFormatter setDateFormat:dateFormat];//hh与HH的区别:分别表示12小时制,24小时制
    NSString *confromTimespStr = [dateFormatter stringFromDate:confromTimesp];
    
    return confromTimespStr;
}

+(NSInteger)getMonth:(NSDate *)date{
    NSCalendar* calendar = [NSCalendar currentCalendar];
    unsigned unitFlags = NSMonthCalendarUnit;
    NSDateComponents* comp = [calendar components:unitFlags fromDate:date];
    return [comp month];
}
+(NSInteger)getYear:(NSDate *)date{
    NSCalendar* calendar = [NSCalendar currentCalendar];
    unsigned unitFlags = NSYearCalendarUnit;
    NSDateComponents* comp = [calendar components:unitFlags fromDate:date];
    return [comp year];
}
+(NSInteger)getDay:(NSDate *)date{
    NSCalendar* calendar = [NSCalendar currentCalendar];
    unsigned unitFlags = NSDayCalendarUnit;
    NSDateComponents* comp = [calendar components:unitFlags fromDate:date];
    return [comp day];
}

+ (NSString *)timeInfoWithDateString:(NSDate *)date {
    
    NSDate *curDate = [NSDate date];
    NSInteger curinterval = [[NSTimeZone systemTimeZone] secondsFromGMTForDate:curDate];
    NSDate *curDate1 = [curDate  dateByAddingTimeInterval: curinterval];
    NSInteger fromeInterval = [[NSTimeZone systemTimeZone] secondsFromGMTForDate:date];
    NSDate *fromeDate = [date  dateByAddingTimeInterval: fromeInterval];
    
    
    NSTimeInterval time = -[fromeDate timeIntervalSinceDate:curDate1];
    NSTimeInterval houseTime = 60*60;
    NSTimeInterval secondsPerDay = 24 * houseTime;
    //假设这是你要比较的date：NSDate *yourDate = ……
    //日历
    
    int month = (int)([self getMonth:curDate] - [self getMonth:date]);
    int year = (int)([self getYear:curDate] - [self getYear:date]);
    int day = (int)([self getDay:curDate] - [self getDay:date]);
    
//     NSLog(@"\nfromeDate:%@,\ncurDate:%@,\nfromeDay = %d \ncurDay = %d",fromeDate,curDate,[self getDay:date],[self getDay:curDate]);
    
    int retTime = 1.0;
    if (time < houseTime) { // 小于一小时
        if (time<=0) {
            return @"";
        }
        retTime = time / 60;
        retTime = retTime <= 0.0 ? 1.0 : retTime;
        return [NSString stringWithFormat:@"%d分钟前", retTime];
        
    }else if (abs(year) == 0 && abs(month) == 0 && day == 1) {
        
        return @"昨天";
        
    }else if (time < secondsPerDay) {
        // 小于一天，也就是今天
        retTime = (time-600) / houseTime;
        retTime = retTime <= 0.0 ? 1.0 : retTime;
        return [NSString stringWithFormat:@"%d小时前", retTime];
        
    }else if ((abs(year) == 0 && abs(month) <= 1)
             || (abs(year) == 1 &&
                 [self getMonth:curDate] == 1
                 && [self getMonth:date] == 12)) {
        // 第一个条件是同年，且相隔时间在一个月内
        // 第二个条件是隔年，对于隔年，只能是去年12月与今年1月这种情况
        int retDay = 0;
        if (year == 0) {
             // 同年
            if (month == 0) {
                 // 同月
                retDay = day;
            }
        }
        if (retDay <= 0) {
            // 获取发布日期中，该月有多少天
            int totalDays = (int)[self numberDaysInMonthOfDate:date];
            // 当前天数 + （发布日期月中的总天数-发布日期月中发布日，即等于距离今天的天数）
            retDay = (int)[self getDay:curDate] + (totalDays - (int)[self getDay:date]);
        }
        if (retDay>10) {
            NSDateFormatter *format=[[NSDateFormatter alloc] init];
            [format setDateFormat:@"yyyy/MM/dd"];
            return [NSString stringWithFormat:@"%@",[format stringFromDate:date]];
        }else{
             return [NSString stringWithFormat:@"%d天前", (abs)(retDay)];
        }
    }else{
        NSDateFormatter *format=[[NSDateFormatter alloc] init];
        [format setDateFormat:@"yyyy/MM/dd"];
        return [NSString stringWithFormat:@"%@",[format stringFromDate:date]];
        
//        if (abs(year) <= 1) {
//            if (year == 0) { // 同年
//                return [NSString stringWithFormat:@"%d个月前", abs(month)];
//            }
//            // 隔年
//            int month = (int)[self getMonth:curDate];
//            int preMonth = (int)[self getMonth:date];
//            if (month == 12 && preMonth == 12) {// 隔年，但同月，就作为满一年来计算
//                return @"1年前";
//            }
//            return [NSString stringWithFormat:@"%d个月前", (abs)(12 - preMonth + month)];
//        }
//        
//        return [NSString stringWithFormat:@"%d年前", abs(year)];
    }
    return @"";
}

+ (NSString *)timeDayInfoWithDateString:(NSDate *)date {
    
    NSDate *curDate = [NSDate date];
    NSInteger curinterval = [[NSTimeZone systemTimeZone] secondsFromGMTForDate:curDate];
    NSDate *curDate1 = [curDate  dateByAddingTimeInterval: curinterval];
    NSInteger fromeInterval = [[NSTimeZone systemTimeZone] secondsFromGMTForDate:date];
    NSDate *fromeDate = [date  dateByAddingTimeInterval: fromeInterval];
    
    
    NSTimeInterval time = -[fromeDate timeIntervalSinceDate:curDate1];
    NSTimeInterval houseTime = 60*60;
    NSTimeInterval secondsPerDay = 24 * houseTime;
    //假设这是你要比较的date：NSDate *yourDate = ……
    //日历
    
    int month = (int)([self getMonth:curDate] - [self getMonth:date]);
    int year = (int)([self getYear:curDate] - [self getYear:date]);
    int day = (int)([self getDay:curDate] - [self getDay:date]);
    
    //     NSLog(@"\nfromeDate:%@,\ncurDate:%@,\nfromeDay = %d \ncurDay = %d",fromeDate,curDate,[self getDay:date],[self getDay:curDate]);
    
    int retTime = 1.0;
    if (time < houseTime) { // 小于一小时
        if (time<=0) {
            return @"";
        }
        retTime = time / 60;
        retTime = retTime <= 0.0 ? 1.0 : retTime;
        return [NSString stringWithFormat:@"今天"];
        
    }else if (abs(year) == 0 && abs(month) == 0 && day == 1) {
        
        return @"昨天";
        
    }else if (time < secondsPerDay) {
        // 小于一天，也就是今天
        retTime = (time-600) / houseTime;
        retTime = retTime <= 0.0 ? 1.0 : retTime;
        return [NSString stringWithFormat:@"今天"];
        
    }else if ((abs(year) == 0 && abs(month) <= 1)
              || (abs(year) == 1 &&
                  [self getMonth:curDate] == 1
                  && [self getMonth:date] == 12)) {
                  // 第一个条件是同年，且相隔时间在一个月内
                  // 第二个条件是隔年，对于隔年，只能是去年12月与今年1月这种情况
                  int retDay = 0;
                  if (year == 0) {
                      // 同年
                      if (month == 0) {
                          // 同月
                          retDay = day;
                      }
                  }
                  if (retDay <= 0) {
                      // 获取发布日期中，该月有多少天
                      int totalDays = (int)[self numberDaysInMonthOfDate:date];
                      // 当前天数 + （发布日期月中的总天数-发布日期月中发布日，即等于距离今天的天数）
                      retDay = (int)[self getDay:curDate] + (totalDays - (int)[self getDay:date]);
                  }
//                  if (retDay>10) {
                      NSDateFormatter *format=[[NSDateFormatter alloc] init];
                      [format setDateFormat:@"yyyy/MM/dd"];
                      return [NSString stringWithFormat:@"%@",[format stringFromDate:date]];
//                  }else{
//                      return [NSString stringWithFormat:@"%d天前", (abs)(retDay)];
//                  }
              }else{
                  NSDateFormatter *format=[[NSDateFormatter alloc] init];
                  [format setDateFormat:@"yyyy/MM/dd"];
                  return [NSString stringWithFormat:@"%@",[format stringFromDate:date]];
                  
                  //        if (abs(year) <= 1) {
                  //            if (year == 0) { // 同年
                  //                return [NSString stringWithFormat:@"%d个月前", abs(month)];
                  //            }
                  //            // 隔年
                  //            int month = (int)[self getMonth:curDate];
                  //            int preMonth = (int)[self getMonth:date];
                  //            if (month == 12 && preMonth == 12) {// 隔年，但同月，就作为满一年来计算
                  //                return @"1年前";
                  //            }
                  //            return [NSString stringWithFormat:@"%d个月前", (abs)(12 - preMonth + month)];
                  //        }
                  //        
                  //        return [NSString stringWithFormat:@"%d年前", abs(year)];
              }
    return @"";
}


+ (NSInteger)numberDaysInMonthOfDate:(NSDate *)date_
{
//    获取发布日期中，该月有多少天
    NSCalendar *calender = [NSCalendar currentCalendar];
    
    NSRange range = [calender rangeOfUnit:NSDayCalendarUnit
                     
                                  inUnit: NSMonthCalendarUnit
                     
                                 forDate: date_];
    
    
    
    return range.length;
    
}

+(NSString*)getComponeWithDateString:(NSString *)time
{
    NSDateFormatter *format=[[NSDateFormatter alloc] init];
    [format setTimeZone:[NSTimeZone systemTimeZone]];
    [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *fromdate1=[format dateFromString:time];
//    NSLog(@"\ntime :%@",time);
    
    return [self timeInfoWithDateString:fromdate1];
}


+(NSDate *)currenDateAddMonth:(NSInteger)month oldDate:(NSDate *)oldDate
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSDateComponents *comps = nil;
    
    comps = [calendar components: NSCalendarUnitYear| NSCalendarUnitMonth| NSCalendarUnitDay fromDate:oldDate];
    
    NSDateComponents *adcomps = [[NSDateComponents alloc] init];
    
    [adcomps setYear:0];
    
    [adcomps setMonth:month];
    
    [adcomps setDay:0];
    
    NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:oldDate options:0];
    
    return newdate;
}

+(NSDate *) currentDateAddDay:(NSInteger)day oldDate:(NSDate *)oldDate
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSDateComponents *comps = nil;
    
    comps = [calendar components: NSCalendarUnitYear| NSCalendarUnitMonth| NSCalendarUnitDay fromDate:oldDate];
    
    NSDateComponents *adcomps = [[NSDateComponents alloc] init];
    
    [adcomps setYear:0];
    
    [adcomps setMonth:0];
    
    [adcomps setDay:day];
    
    NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:oldDate options:0];
    
    return newdate;
}

+ (NSString *)formateDate:(NSString *)dateString withFormate:(NSString *) formate
{
    
    @try {
        //实例化一个NSDateFormatter对象
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:formate];
        
        NSDate * nowDate = [NSDate date];
        
        /////  将需要转换的时间转换成 NSDate 对象
        NSDate * needFormatDate = [dateFormatter dateFromString:dateString];
        /////  取当前时间和转换时间两个日期对象的时间间隔
        /////  这里的NSTimeInterval 并不是对象，是基本型，其实是double类型，是由c定义的:  typedef double NSTimeInterval;
        NSTimeInterval time = [nowDate timeIntervalSinceDate:needFormatDate];
        
        //// 再然后，把间隔的秒数折算成天数和小时数：
        
        NSString *dateStr = @"";
        
        if (time<=60) {  //// 1分钟以内的
            dateStr = @"刚刚";
        }else if(time<=60*60){  ////  一个小时以内的
            
            int mins = time/60;
            dateStr = [NSString stringWithFormat:@"%d分钟前",mins];
            
        }else if(time<=60*60*24){   //// 在两天内的
            
            [dateFormatter setDateFormat:@"YYYY/MM/dd"];
            NSString * need_yMd = [dateFormatter stringFromDate:needFormatDate];
            NSString *now_yMd = [dateFormatter stringFromDate:nowDate];
            
            [dateFormatter setDateFormat:@"HH:mm"];
            if ([need_yMd isEqualToString:now_yMd]) {
                //// 在同一天
                dateStr = [NSString stringWithFormat:@"今天 %@",[dateFormatter stringFromDate:needFormatDate]];
            }else{
                ////  昨天
                dateStr = [NSString stringWithFormat:@"昨天 %@",[dateFormatter stringFromDate:needFormatDate]];
            }
        }else {
            
            [dateFormatter setDateFormat:@"yyyy"];
            NSString * yearStr = [dateFormatter stringFromDate:needFormatDate];
            NSString *nowYear = [dateFormatter stringFromDate:nowDate];
            
            if ([yearStr isEqualToString:nowYear]) {
                ////  在同一年
                [dateFormatter setDateFormat:@"MM月dd日 HH:mm"];
                dateStr = [dateFormatter stringFromDate:needFormatDate];
            }else{
                [dateFormatter setDateFormat:@"yyyy/MM/dd"];
                dateStr = [dateFormatter stringFromDate:needFormatDate];
            }
        }
        
        return dateStr;
    }
    @catch (NSException *exception) {
        return @"";
    }
}

+(NSString *) numberMonthToEnglish:(NSInteger)month
{
    if (month == 1) {
        return @"January";
    }else if (month == 2) {
        return @"February";
    }else if (month == 3) {
        return @"March";
    }else if (month == 4) {
        return @"April";
    }else if (month == 5) {
        return @"May";
    }else if (month == 6) {
        return @"June";
    }else if (month == 7) {
        return @"July";
    }else if (month == 8) {
        return @"August";
    }else if (month == 9) {
        return @"September";
    }else if (month == 10) {
        return @"October";
    }else if (month == 11) {
        return @"November";
    }else if (month == 12) {
        return @"December";
    }
    return @"";
}

+(NSString *) getEnglishDate:(NSDate *)date
{
    
    NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents* comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay  fromDate:date];
    return [NSString stringWithFormat:@"%@ %ld,%ld", [NSDateExtra numberMonthToEnglish:comp.month], comp.day, comp.year];
}

+ (NSDate *) currentDateSubtractMonth:(NSInteger)month oldDate:(NSDate *)oldDate{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents * adcomps = [[NSDateComponents alloc] init];
    [adcomps setYear:0];
    [adcomps setMonth:-month];
    [adcomps setDay:0];
    NSDate *newDate = [calendar dateByAddingComponents:adcomps toDate:oldDate options:0];
    return newDate;
}
+ (NSDate *) currentDateSubtractDay:(NSInteger)day oldDate:(NSDate*)oldDate{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents * adcomps = [[NSDateComponents alloc] init];
    [adcomps setYear:0];
    [adcomps setMonth:0];
    [adcomps setDay:-day];
    NSDate *newDate = [calendar dateByAddingComponents:adcomps toDate:oldDate options:0];
    return newDate;
}

+(NSString *)getMMSSFromSS:(NSString *)totalTime{

    NSInteger seconds = [totalTime integerValue];

    //format of hour
    NSString *str_hour = [NSString stringWithFormat:@"%02ld",seconds/3600];
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02ld",(seconds%3600)/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60];
    //format of time
    
    if ([str_hour floatValue] > 0) {
        return [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
    }else{
        return [NSString stringWithFormat:@"%@:%@",str_minute,str_second];
    }
}

@end
